package com.cogent.validator.facade.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.cogent.validator.IValidator;
import com.cogent.validator.cache.ValidatorCache;
import com.cogent.validator.domain.ValidatorDomain;
import com.cogent.validator.facade.ValidatorFacade;
import com.cogent.validator.impl.MaxLengthValidator;
import com.cogent.validator.impl.RegularValidator;
import com.cogent.validator.impl.RequiredValidator;
import com.cogent.validator.impl.TextAreaRepeatCheckValidator;

public class ValidatorFacadeImpl implements ValidatorFacade {
	private static Map VALIDATOR_MAP = new HashMap();
	static{
		VALIDATOR_MAP.put("regular", new RegularValidator());
		VALIDATOR_MAP.put("required", new RequiredValidator());
		VALIDATOR_MAP.put("maxlength", new MaxLengthValidator());
		VALIDATOR_MAP.put("textarearepeatcheck", new TextAreaRepeatCheckValidator());
	}
	
	public String getScript(String formName) {
		return getScript(ValidatorCache.get(formName));
	}
	
	public String getScript(String formName, List sublist) {
		return getScript(ValidatorCache.get(formName), sublist);
	}
	
	public String getScript(List validatorList, List sublist) {
		return getScript(validatorList, sublist, "DYNTABLE");
	}
	
	public String getScript(List validatorList, List sublist, String prefix) {
		if(sublist.size() == 0)
			return getScript(validatorList);
		StringBuffer str = new StringBuffer();
		str.append("<script type=\"text/javascript\">");
		str.append("\nfunction checkForm(){");
		str.append("\n\terrmsg = new ErrMessage(\"\", \"\");");
		str.append(genScript(validatorList));
		str.append("\n\tvar curnum = 0;");
		str.append("\n\twhile(true){");
		str.append("\n\t\ttry{");
		String temp = ".";
		if(prefix.equals("QUERYDYNTABLE"))
			temp = "~";
		str.append("\n\t\t\tvar curobj = document.getElementById('" + prefix + "[' + curnum + ']" + temp + "id');");
		str.append("\n\t\t\tif(curobj == null)");
		str.append("\n\t\t\t\treturn true;");
		str.append("\n\t\t}catch(e){");
		str.append("\n\t\t\treturn true;");
		str.append("\n\t\t}");
		Iterator it = sublist.iterator();
		while(it.hasNext()) {
			ValidatorDomain validatorDomain = (ValidatorDomain)it.next();
			validatorDomain.setPrefix(prefix + "[");
			IValidator validator = (IValidator)VALIDATOR_MAP.get(validatorDomain.getValidator());
			str.append("\n\t\t").append(validator.getScript(validatorDomain));
		}
		str.append("\n\t\tif(errmsg.notemsg.length>0){");
		str.append("\n\t\t\talert(errmsg.notemsg);");
		str.append("\n\t\t\tgetObj(errmsg.focus).focus();");
		str.append("\n\t\t\treturn false;");
		str.append("\n\t\t}");
		str.append("\n\t\tcurnum++;");
		str.append("\n\t}");
		str.append("\n}");
		str.append("\n</script>");
		
		return str.toString();
	}
	
	public String getScript(List validatorList) {
		StringBuffer str = new StringBuffer();
		str.append("<script type=\"text/javascript\">");
		str.append("\nfunction checkForm(){");
		str.append("\n\terrmsg = new ErrMessage(\"\", \"\");");
		str.append(genScript(validatorList));		
		str.append("\n\treturn true;");
		str.append("\n}");
		str.append("\n</script>");
		
		return str.toString();
	}
	
	private String genScript(List validatorList) {
		StringBuffer str = new StringBuffer();
		Iterator it = validatorList.iterator();
		while(it.hasNext()) {
			ValidatorDomain validatorDomain = (ValidatorDomain)it.next();
			IValidator validator = (IValidator)VALIDATOR_MAP.get(validatorDomain.getValidator());
			str.append("\n\t").append(validator.getScript(validatorDomain));
		}
		str.append("\n\tif(errmsg.notemsg.length>0){");
		str.append("\n\t\talert(errmsg.notemsg);");
		str.append("\n\t\tgetObj(errmsg.focus).focus();");
		str.append("\n\t\treturn false;");
		str.append("\n\t}");
		return str.toString();
	}
	
	public String validator(String formName, HashMap dataMap) {
		return validator(ValidatorCache.get(formName), dataMap);
	}
	
	public String validator(List validatorList, HashMap dataMap) {
		StringBuffer str = new StringBuffer();
		Iterator it = validatorList.iterator();
		while(it.hasNext()) {
			ValidatorDomain validatorDomain = (ValidatorDomain)it.next();
			IValidator validator = (IValidator)VALIDATOR_MAP.get(validatorDomain.getValidator());
			String temp = validator.validator(dataMap, validatorDomain);
			if(temp.length() > 0) {
				if(str.length() > 0)
					str.append("\\n");
				str.append(temp);
			}
		}
		
		return str.toString();
	}

}
